package com.roubsite.admin.action;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;

import com.roubsite.admin.bean.ZTree;
import com.roubsite.admin.dao.rsMenu.IRsMenuDao;
import com.roubsite.admin.dao.rsMenu.RsMenuDao;
import com.roubsite.admin.dao.rsRoleMenu.IRsRoleMenuDao;
import com.roubsite.admin.dao.rsRoleMenu.RsRoleMenuDao;
import com.roubsite.database.annotation.Dao;
import com.roubsite.database.bean.Record;
import com.roubsite.database.dao.DataSet;
import com.roubsite.security.annotation.RSSecRetType;
import com.roubsite.security.annotation.RSSecurity;
import com.roubsite.utils.JsonUtils;
import com.roubsite.web.action.RSAction;

@SuppressWarnings("rawtypes")
public class RsRoleMenuAction extends RSAction {

	@Dao(impl = RsRoleMenuDao.class, dataSource = "dataSource")
	IRsRoleMenuDao rsRoleMenuDao;
	@Dao(impl = RsMenuDao.class, dataSource = "dataSource")
	IRsMenuDao rsMenu;

	@RSSecurity
	@Override
	public void execute() throws Exception {
		String role_id = this.I("role_id");
		List ds = rsMenu.queryAll();
		this.assign("role_id", role_id);
		this.assign("menuDataset", JsonUtils.convertToString(ds));
		this.display("/rsRoleMenu/listRsRoleMenu.html");
	}

	@RSSecurity(retType = RSSecRetType.retJson)
	public void doQuery() throws IOException {
		Record record = new Record();
		Map paramSet = (Map) this.getParamSet(false);
		record.fromBeanMap((Map) paramSet.get("__data__"));
		DataSet ret = rsRoleMenuDao.queryd(record, (int) paramSet.get("__offset__"), (int) paramSet.get("__limit__"));
		this.print(JsonUtils.convertToString(ret));
	}

	@RSSecurity(retType = RSSecRetType.retJson)
	public void doDel() throws Exception {
		String jsonString = (String) this.getParamSet(true);
		List ids = JsonUtils.readToObject(jsonString, ArrayList.class);
		int r = rsRoleMenuDao.del(ids);
		Map ret = new HashMap();
		ret.put("status", r);
		this.print(JsonUtils.convertToString(ret));
	}

	@RSSecurity(retType = RSSecRetType.retJson)
	public void doEdit() throws ServletException, IOException {
		String menuIds = this.I("menuIds");
		String role_id = this.I("role_id");
		// 删除所有权限
		rsRoleMenuDao.delByRoleId(role_id);

		List menuIdList = JsonUtils.readToObject(menuIds, ArrayList.class);
		int status = rsRoleMenuDao.insertRoleMenu(role_id, menuIdList);
		Map ret = new HashMap();
		ret.put("status", status);
		this.print(JsonUtils.convertToString(ret));

	}

	@RSSecurity
	public void doGetRoleMenu() throws IOException, ServletException {
		String role_id = this.I("role_id");
		List<ZTree> tree = rsRoleMenuDao.getRoleMenuTree(role_id);
		this.assign("role_id", role_id);
		this.assign("tree", JsonUtils.convertToString(tree));
		this.display("/rsRoleMenu/roleMenuTree.html");
	}
}
